﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace BoxDiary
{
    public partial class DiaryController : UserControl
    {
        public DiaryController()
        {
            InitializeComponent();

            textBox1.UseSystemPasswordChar = true;
        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void DiaryController_Load(object sender, EventArgs e)
        {
            richTextBox1.Hide();


            //将所有日记加载到缓冲区
            string path = $@"{Application.StartupPath}\Diary";
            List<string> allfile = new List<string>();
            foreach (string filename in Directory.GetFiles(path))
            {
                FileInfo file = new FileInfo(filename);
                allfile.Add(file.Name);
            }

            foreach (string a in allfile)
            {
                listBox1.Items.Add(a.Split('.')[0]);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string path = $@"{Application.StartupPath}\Diary\{listBox1.SelectedItem.ToString()}.rtf";
                File.Copy(path,
                    $@"{Application.StartupPath}\Diary\{listBox1.SelectedItem.ToString()} Copy.rtf");
                listBox2.Items.Add("复制完成。");
            }
            catch
            {
                listBox2.Items.Add("复制失败。");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string path = $@"{Application.StartupPath}\Diary\{listBox1.SelectedItem.ToString()}.rtf";
                File.Delete(path);
                listBox2.Items.Add("删除完成。");
            }
            catch
            {
                listBox2.Items.Add("删除失败。");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string path = $@"{Application.StartupPath}\Diary\{listBox1.SelectedItem.ToString()}.rtf";
                richTextBox1.LoadFile(path);
                string rtfDocument = richTextBox1.Text.Split(';')[1];
                string result = AES.EncryptNew.AESDecrypt(rtfDocument, textBox1.Text);
                richTextBox1.Text = result;
                //可能要获取的路径名
                string localFilePath = "";
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                //设置文件类型
                saveFileDialog.Filter = "txt files(*.txt)|*.txt|rtf files(*.rtf)|*.rtf";
                //设置默认文件名（可以不设置）
                saveFileDialog.FileName = "我的日记";
                //获取或设置一个值，该值指示如果用户省略扩展名，文件对话框是否自动在文件名中添加扩展名。（可以不设置）
                saveFileDialog.AddExtension = true;
                //保存对话框是否记忆上次打开的目录
                saveFileDialog.RestoreDirectory = true;

                // Show save file dialog box
                DialogResult dresult = saveFileDialog.ShowDialog();
                //点了保存按钮进入
                if (dresult == DialogResult.OK)
                {
                    //获得文件路径
                    localFilePath = saveFileDialog.FileName.ToString();
                    richTextBox1.SaveFile(localFilePath);
                    listBox2.Items.Add("导出成功。");
                }
                else
                {
                    listBox2.Items.Add("用户取消操作。");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show($"导出日记遇到问题，可能是文件正在使用中或者秘钥错误。\n错误原因：{ex.Message}。","导出错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            MessageBox.Show("批量功能尚在开发中，预计下个版本加入。","未开发",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }
    }
}
